<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    append-to-body
    :close-on-click-modal="false"
    @close="closeDialog()"
    :visible="visible"
    width="600px"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="分类名" prop="categoryName">
        <el-input
          v-model="dataForm.categoryName"
          placeholder="请输入分类名"
        ></el-input>
      </el-form-item>

      <el-row>
        <el-col :span="12">
          <el-form-item label="排序" prop="sort">
            <el-input-number
              v-model="dataForm.sort"
              controls-position="right"
              :min="0"
              style="width:90%"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="是否显示" prop="status">
            <el-radio-group v-model="dataForm.status">
              <el-radio label="1" border>启用</el-radio>
              <el-radio label="0" border>关闭</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="closeDialog">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" v-if="canSubmit"
        >确定</el-button
      >
    </span>
  </el-dialog>
</template>

<script>
import { getObj, addObj, putObj } from "@/api/article/category";

export default {
  props:{
    visible: {
      type: Boolean,
      default: false
    },
  },
  data() {
    return {
      canSubmit: false,
      dataForm: {
        status:'1'
      },
      dataRule: {
        categoryName: [
          { required: true, message: "分类名不能为空", trigger: "blur" },
        ],
        sort: [
          { required: true, message: "排序不能为空", trigger: "blur" },
        ],
        status: [
          { required: true, message: "是否显示不能为空", trigger: "blur" },
        ]
      },
    };
  },
  methods: {
    init(id) {
      this.canSubmit = true;
      if(id){
        getObj(id).then((response) => {
          this.dataForm = response.data.data;
        });
      }
      
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.canSubmit = false;
          if (this.dataForm.id) {
            putObj(this.dataForm)
              .then((data) => {
                this.$notify.success("修改成功");
                this.$emit("refreshDataList");
                this.closeDialog()
              })
              .catch(() => {
                this.canSubmit = true;
              });
          } else {
            addObj(this.dataForm)
              .then((data) => {
                this.$notify.success("添加成功");
                this.$emit("refreshDataList");
                this.closeDialog()
              })
              .catch(() => {
                this.canSubmit = true;
              });
          }
        }
      });
    },
    //重置表单
    closeDialog() {
      this.$emit('update:visible', false)
    },
  },
};
</script>
